//


#import "NT_LeftHeaderView.h"
#import "NT_CategoryEditViewController.h"

@implementation NT_LeftHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        [self initUI];
    }return self;
}
//MARK:
- (void)initUI {
    //
    self.backview = ({
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        view;
    });
    self.titleLabel = ({
        UILabel *label = [[UILabel alloc]init];
        label.textColor = rgba(90, 90, 90, 1);
        label.font = FontPFRegular(kRelativeWidth(15));
        label.text = @"";
        [self.backview addSubview:label];
        label;
    });
    self.editButton = ({
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"Edit" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:FontPFRegular(kRelativeWidth(15))];
        [button addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
        button.hidden = YES;
        [self.backview addSubview:button];
        button;
    });
    [self setNeedsLayout];
    
}
//
- (void)setNeedsLayout {
    [super setNeedsLayout];
    
    [self.backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.right.mas_offset(0);
        make.bottom.mas_equalTo(self).offset(-0.7);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(kRelativeHeight(10));
        make.left.mas_offset(kRelativeWidth(12));
        make.height.mas_equalTo(kRelativeHeight(20));
    }];
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(kRelativeHeight(10));
        make.right.mas_offset(-kRelativeWidth(12));
        make.height.mas_equalTo(kRelativeHeight(20));
    }];
}


//
- (void)Action {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"hidder" object:nil];
      NT_CategoryEditViewController *vc = [[NT_CategoryEditViewController alloc]init];
     UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
    [[self currentViewController] presentViewController:nvc animated:YES completion:nil];
}

//获取Window当前显示的ViewController
- (UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
//        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
    return vc;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
